import { defineStore } from 'pinia';
import type { UserInfoStates, ILogin } from '@/types/login';
// import { router } from '@/router';
import { RoleEnum } from '@/enums/roleEnum';
export const useUserStore = defineStore("UserStore",{
    persist: {
		enabled: true,
		strategies: [
			{
				key: 'UserStore',
				storage: window?.localStorage,
			},
		],
	},

    state: (): UserInfoStates => ({
		userInfo: {
			userName: '',
			photo: '',
			time: '',
			roles: [],
			authBtnList: [],
			id: '',
		},
		token: '',
        roleList:[]
	}),

    getters: {
		userId: (state: { userInfo: { userId: any; }; }) => state.userInfo?.userId,
		avatar: (state: { userInfo: { avatar: any; }; }) => state.userInfo?.avatar,
		username: (state: { userInfo: { username: any; }; }) => state.userInfo?.username,
        getUserInfo:(state:UserInfoStates|null)=>state.userInfo
	},

    actions: {
		async setUserInfo(data: Partial<ILogin>) {
			this.userInfo = { ...this.userInfo, ...data };
			// const res = await loginAPI(data);
		},

		async setToken(token: string) {
			// 存储用户信息到浏览器缓存
			this.token = token;
		},

		// 获取用户详情
		async getUserInfo({ params = {}, ...options } = {}) {
			console.log('params,options', options, params);
			// const res = await getUserInfo(params, options)

			// if (!res?.success) {
			//   throw new Error('获取用户信息失败')
			// }

			// if (!res?.data) {
			//   throw new Error('获取用户信息失败, 没有获取到数据')
			// }

			// const data = res.data
			// this.userInfo = {
			//   ...data,
			//   userId: data.id,
			//   avatar: data.avatar || defaultAvatar,
			//   username: data.username,
			// }

			// return this.userInfo
		},

		async logout({ silenced = false, redirect = null } = {}) {
			console.log('silenced,redirect', silenced, redirect);
			// this.removeToken()
			// this.userInfo = {}
			// this.resumeInfo = {}

			// if (!silenced) {
			// 	router.replaceAll({
			// 	force: true,
			// 	path: '/login',
			// 	query: {
			// 		redirect,
			// 	},
			// 	})
			// }
		},
	},
})